Designing Elixir Systems With OTP by James Edward Gray II

Designing Elixir Systems With OTP by James Edward Gray II

Author:James Edward Gray II
Language: eng
Format: epub
Tags: Pragmatic Bookshelf
Publisher: Pragmatic Bookshelf


Try the Quiz Manager

In this section, we’re going to put the quiz through its paces. Part of writing good code is building the infrastructure to support learning and exploration. We’re going to create a trivial module to let new users and developers alike explore our features in the console.

Let’s create a simple math quiz with mastery of two and a template for single-digit addition. None of this code will be new to you:

Boundary/lib/mastery/examples/math.ex

​ ​defmodule​ Mastery.Examples.Math ​do​

​ alias Mastery.Core.Quiz

​ ​def​ template_fields() ​do​

​ [

​ ​name:​ ​:single_digit_addition​,

​ ​category:​ ​:addition​,

​ ​instructions:​ ​"​​Add the numbers"​,

​ ​raw:​ ​"​​<%= @left %> + <%= @right %>"​,

​ ​generators:​ addition_generators(),

​ ​checker:​ &addition_checker/2

​ ]

​ ​end​

​ ​def​ addition_checker(substitutions, answer) ​do​

​ left = Keyword.fetch!(substitutions, ​:left​)

​ right = Keyword.fetch!(substitutions, ​:right​)

​ to_string(left + right) == String.trim(answer)

​ ​end​

​ ​def​ addition_generators() ​do​

​ %{​left:​ Enum.to_list(0..9), ​right:​ Enum.to_list(0..9)}

​ ​end​

​ ​def​ quiz_fields() ​do​

​ %{ ​mastery:​ 2, ​title:​ ​:simple_addition​}

​ ​end​

​ ​def​ quiz() ​do​

​ quiz_fields()

​ |> Quiz.new

​ |> Quiz.add_template(template_fields())

​ ​end​

​ ​end​

We have separate functions for quiz and template fields. We also have a function to roll that up into a new quiz. Now we have a few tools that will make our module easy to test. Open up a new IEx session, or at least recompile. Then, you can alias the modules we’ll need, like this:

​ ​iex(1)>​ alias Mastery.Examples.Math

​ Mastery.Examples.Math

​ ​iex(2)>​ alias Mastery.Boundary.QuizManager

​ Mastery.Boundary.QuizManager

We alias Math, the example quiz we just built for convenience, and QuizManager, the server layer for building quizzes. Now, we need to start the quiz:

​ ​iex(3)>​ GenServer.start_link QuizManager, %{}, ​name:​ QuizManager

​ {:ok, #PID<0.123.0>}

We need to be able to access our server, perhaps from a web layer so we’ll need to be able to reference it by name. We’ll use the name of the module, which means we’ll only have one copy of QuizManager. The start_link has three arguments, the module that has the GenServer implementation, the empty map that will eventually contain our quizzes, and options. We use the :name option to specify the name for our new server. Now, we can use it:

​ ​iex(4)>​ QuizManager.build_quiz ​title:​ ​:quiz​

​ :ok

​ ​iex(5)>​ QuizManager.add_template ​:quiz​, Math.template_fields

​ :ok

You can see the smoother API we offer from this layer. We build a quiz, strictly with functions:

​ ​iex(6)>​ QuizManager.lookup_quiz_by_title ​:quiz​

​ %Mastery.Core.Quiz{ ... }

Nice! That much works. We can see the individual fields of the quiz we added. That’s more than half of our server. Now admin users can establish new quizzes. It’s time to switch to the rest of our server layer, the part for taking quizzes and answering questions.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.